{$CLEO .qa}
{$USE CLEO+}
{$USE newOpcodes}
{$USE ini}

0000: NOP

script_name "WrkPeds"

// By ArtemQa146

// 20@ ped - 10@ render
// 21@ ped - 11@ render
// 22@ ped - 12@ render
// 23@ ped - 13@ render
// 24@ ped - 14@ render

const
    Peds_Find = 19@
    Ped_Skin = 1@
    Ped_Select = 17@
    Bit_sets = 31@
    Select_Hand = 1
    Number_Peds = 4
    iBuffer = 29@
    Pause_Time = 300
    Pause = 1000
end

0085: iBuffer = 0@ // (int)
0@ = -1

while true
wait 50
if
    Player.Defined($PLAYER_CHAR)
then
gosub @Ped_20
gosub @Realese_animation
    if
        not $ACTIVE_INTERIOR == 0
    then
        gosub @References_remove
        continue
    end

0E40: get_current_hour 2@
0D59: get_current_weather_to 1@
if and
    2@ >= 7
    2@ < 18
    not 1@ == 8 // not weather rain
    not 1@ == 16 // not weather rain 2
then
    if or
        856D: not actor 20@ defined
        856D: not actor 21@ defined
        856D: not actor 22@ defined
        856D: not actor 23@ defined
        856D: not actor 24@ defined
    then
    Peds_Find = 0
    while 0EA7: get_any_char_no_save_recursive Peds_Find progress_to Peds_Find char_to 0@
    4@ = 50.0
    if 82CB:   not actor 0@ bounding_sphere_visible
    then 4@ = 20.0
    end
        080E: get_actor 0@ ped_event_to 1@
        if and
            not 1@ == 9 // ped damaged
            80DF: not actor 0@ driving
            not is_char_script_controlled 0@
            not locate_char_distance_to_char 0@ char $PLAYER_ACTOR radius 4@
            locate_char_distance_to_char 0@ char $PLAYER_ACTOR radius 90.0
        then
            0470: 3@ = actor 0@ current_weapon
            0EFF: get_char_simplest_active_task 0@ id_to 1@ pointer_to 2@
            if and
                3@ == 0 //unarmed
                1@ == 900 //walk
                not Actor.Animation(0@) == "sprint_panic"
                not Actor.Animation(0@) == "woman_runpanic"
                not Actor.Animation(0@) == "idle_chat"
            then
                gosub @Ped_skin
                if
                    Ped_Skin == true
                then
                    gosub @Load_animation
                    if
                        056D: actor 0@ defined
                    then
                        if
                            not is_char_script_controlled 0@
                        then
                        
                            for Ped_Select = 0 to Number_Peds step 1
                                if
                                    856D: not actor 20@(Ped_Select,1i) defined
                                then

                                    if and
                                        Ped_Select == 0 // 
                                        02F2:   actor 0@ model == 71 // security
                                    then
                                        0085: 20@(Ped_Select,1i) = 0@ // (int)
                                        0E0B: mark_char_as_needed 20@(Ped_Select,1i)

                                        // ,   
                                        3@ = 24

                                        gosub @Man_Case
                                        gosub @Ped_case
                                        wait Pause
                                        break
                                    end

                                    if and
                                        Ped_Select == 1 //   
                                        02F2:   actor 0@ model == 153 // worker boss
                                    then
                                        0085: 20@(Ped_Select,1i) = 0@ // (int)
                                        0E0B: mark_char_as_needed 20@(Ped_Select,1i)

                                        // ,   
                                        0209: 3@ = random_int_in_ranges 1 3
                                        if 3@ == 1
                                        then 3@ = 24
                                        else 3@ = 34
                                        end

                                        gosub @Man_Case_Standart
                                        gosub @Ped_case_standart
                                        wait Pause
                                        break
                                    end

                                    if and
                                        Ped_Select == 2 // 
                                        82F2:   not actor 0@ model == 71 // security
                                        82F2:   not actor 0@ model == 153 // worker boss
                                    then
                                        0085: 20@(Ped_Select,1i) = 0@ // (int)
                                        0E0B: mark_char_as_needed 20@(Ped_Select,1i)
                                        0209: 3@ = random_int_in_ranges 1 3
                                        if 3@ == 1
                                        then 3@ = 24
                                        else 3@ = 34
                                        end
                                        
                                        gosub @Work_tools
                                        gosub @Ped_case
                                        wait Pause
                                        break
                                    end


                                    if and
                                        Ped_Select == 3 // 
                                        82F2:   not actor 0@ model == 71 // security
                                        82F2:   not actor 0@ model == 153 // worker boss
                                    then
                                        0085: 20@(Ped_Select,1i) = 0@ // (int)
                                        0E0B: mark_char_as_needed 20@(Ped_Select,1i)
                                        0209: 3@ = random_int_in_ranges 1 3
                                        if 3@ == 1
                                        then 3@ = 24
                                        else 3@ = 34
                                        end

                                        gosub @Work_tools
                                        gosub @Ped_case
                                        wait Pause
                                        break
                                    end

                                    if and
                                        Ped_Select == 4 //  
                                        82F2:   not actor 0@ model == 71 // security
                                        82F2:   not actor 0@ model == 153 // worker boss
                                    then
                                        0085: 20@(Ped_Select,1i) = 0@ // (int)
                                        0E0B: mark_char_as_needed 20@(Ped_Select,1i)
                                        2@ = 6
                                        2@ += 84
                                        2@ *= 4
                                        read_struct_offset 1@ iBuffer offset 2@ size 4   // Read ID of Model 2
                                        gosub @Ped_Plank
                                        wait Pause
                                        break
                                    end
                                end
                            end
                            break
                        end
                    end
                end
            end
        end
    end
end
end
end
end

//  ,   

:Man_Case
0209: 2@ = random_int_in_ranges 1 6
if 2@ == 1
then 0AF0: 1@ = read_int_from_ini_file "cleo\PedsModels.ini" section "Models" key "cop1"
end
if 2@ == 2
then 0AF0: 1@ = read_int_from_ini_file "cleo\PedsModels.ini" section "Models" key "cop2"
end
if 2@ == 3
then 
    2@ = 9
    2@ += 84
    2@ *= 4
    read_struct_offset 1@ iBuffer offset 2@ size 4   // Read ID of Model 2
end
if 2@ == 4
then
    2@ = 10
    2@ += 84
    2@ *= 4
    read_struct_offset 1@ iBuffer offset 2@ size 4   // Read ID of Model 2
end
if 2@ == 5
then 
    2@ = 11
    2@ += 84
    2@ *= 4
    read_struct_offset 1@ iBuffer offset 2@ size 4   // Read ID of Model 2
end
return

:Man_Case_Standart
1@ = 1210
4@ = 0.28
5@ = 0.07
6@ = -0.01
7@ = 270.0
8@ = 1.0
return

:Work_tools
0209: 2@ = random_int_in_ranges 1 14
if or
    2@ == 6
    2@ == 9
    2@ == 10
    2@ == 11
then 
    0209: 2@ = random_int_in_ranges 1 6
end
if or
    2@ == 3
    2@ == 4
    2@ == 5
    2@ == 7
    2@ == 8
then
    3@ = 24
end
2@ += 84
2@ *= 4
read_struct_offset 1@ iBuffer offset 2@ size 4   // Read ID of Model 2
return

//  


:Ped_Plank
0F02:  20@(Ped_Select,1i) special_model 1@ bone 34 offset 0.01 0.035 0 rotation 0 180.0 180.0 scale 10@(Ped_Select,1i) // bone 34 left hand, 24 right hand, 9 had, 3 tors
0E30: set_render_object_auto_hide 10@(Ped_Select,1i) dead 1 weapon 1 car 1
0A1A: actor 20@(Ped_Select,1i) perform_walk_animation "WorkPlank" IFP "PedEventsMod" framedelta 4.0 loopA 1 lockX 0 lockY 0 lockF 1 -1 ms // versionC
return

:Ped_case
0F02:  20@(Ped_Select,1i) special_model 1@ bone 3@ offset 0 0 0 rotation 0 0 0 scale 10@(Ped_Select,1i) // bone 34 left hand, 24 right hand, 9 had, 3 tors
0E30: set_render_object_auto_hide 10@(Ped_Select,1i) dead 1 weapon 1 car 1
return

:Ped_case_standart
model.Load(1@)
if
    not model.Available(1@)
then
    repeat
    wait 0
    model.Load(1@)
    until model.Available(1@)
end
038B: load_requested_models
if
    056D:   actor 20@(Ped_Select,1i) defined
then
    0E2E: create_render_object_to_char_bone 20@(Ped_Select,1i) model 1@ bone 3@ offset 4@ 5@ 6@ rotation 0 7@ 0 store_to 10@(Ped_Select,1i)
    set_render_object_scale 10@(Ped_Select,1i) 8@ 8@ 8@
    0E30: set_render_object_auto_hide 10@(Ped_Select,1i) dead 1 weapon 1 car 1
end
model.Destroy(1@)
return

//   


:Ped_20
for Ped_Select = 0 to Number_Peds step 1
wait 50
    if
        056D: actor 20@(Ped_Select,1i) defined
    then
        0A96: 3@ = ped 20@(Ped_Select,1i) struct
        0D4E: 2@ = read_struct 3@ offset 0x470 size 1
        if 08B7:   test 2@ bit 3 //      
        then
            08C3: clear 2@ bit 3
            0E28: write_struct 3@ offset 0x470 size 1 value 2@ //      
            10@(Ped_Select,1i) = -1
            gosub @PedsMod_Delete_Render
            continue
        end
        if and
            not actor.Dead(20@(Ped_Select,1i))
            is_char_script_controlled 20@(Ped_Select,1i)
            locate_char_distance_to_char 20@(Ped_Select,1i) char $PLAYER_ACTOR radius 120.0
        then
            //    
            if
                Ped_Select == 4
            then
                0EFF: get_char_simplest_active_task 20@(Ped_Select,1i) id_to 3@ pointer_to 2@
                if and
                    02CB:   actor 20@(Ped_Select,1i) bounding_sphere_visible
                    3@ == 900
                    not 3@ == 401
                    not Actor.Animation(20@(Ped_Select,1i)) == "WorkPlank"
                    timerb > 5000
                then
                    0A1A: actor 20@(Ped_Select,1i) perform_walk_animation "WorkPlank" IFP "PedEventsMod" framedelta 4.0 loopA 1 lockX 0 lockY 0 lockF 0 -1 ms // versionC
                    timerb = 0
                end
            end

            080E: get_actor 20@(Ped_Select,1i) ped_event_to 1@
            if or
                1@ == 49 // shoot close ped
                0E47: is_char_fighting 20@(Ped_Select,1i)
                actor.Driving(20@(Ped_Select,1i))
                82D8:   not actor 20@(Ped_Select,1i) current_weapon == 0
            then
                gosub @PedsMod_Delete_Render
            end
        else
            gosub @PedsMod_Delete_Render
        end
    end
end
return


//      
:PedsMod_Delete_Render
if
    056D: actor 20@(Ped_Select,1i) defined
then
    if
        not locate_char_distance_to_char 20@(Ped_Select,1i) char $PLAYER_ACTOR radius 50.0
    then
        if
            0E0A: is_char_script_controlled 20@(Ped_Select,1i)
        then
            01C2: remove_references_to_actor 20@(Ped_Select,1i) // Like turning an actor into a random pedestrian
        end
    else
        if
            0E0A: is_char_script_controlled 20@(Ped_Select,1i)
        then
            if
                not 10@(Ped_Select,1i) <= -1
            then
                0E31: set_render_object_visible 10@(Ped_Select,1i) 0
            end
            if
                not actor.Dead(20@(Ped_Select,1i))
            then
                if
                    Ped_Select == 4
                then
                    0E97: clear_char_secondary_tasks 20@(Ped_Select,1i)
                end
            end
            01C2: remove_references_to_actor 20@(Ped_Select,1i) // Like turning an actor into a random pedestrian
        end
    end
end
10@(Ped_Select,1i) = -1
20@(Ped_Select,1i) = -1
return

//  
:Load_animation
if
    84EE: not animation "PedEventsMod" loaded
then
    repeat
    wait 0
    04ED: load_animation  "PedEventsMod"
    until 04EE:   animation "PedEventsMod" loaded
end
return


:Ped_skin
Ped_skin = false
if or
    02F2:   actor 0@ model == 16 // worker
    02F2:   actor 0@ model == 27 // worker
    02F2:   actor 0@ model == 50 // worker mechanik
    02F2:   actor 0@ model == 260 // worker
    02F2:   actor 0@ model == 153 // worker boss
    02F2:   actor 0@ model == 71 // security
then
    Ped_skin = true
end
return


//     
:References_remove
for Ped_Select = 0 to Number_Peds step 1
    if
        056D:   actor 20@(Ped_Select,1i) defined
    then
        01C2: remove_references_to_actor 20@(Ped_Select,1i) // Like turning an actor into a random pedestrian
        20@(Ped_Select,1i) = -1
        10@(Ped_Select,1i) = -1
    end
end
return

//      ,   
:Realese_animation
if and
    856D: not actor 20@ defined
    856D: not actor 21@ defined
    856D: not actor 22@ defined
    856D: not actor 23@ defined
    856D: not actor 24@ defined
then
    if
        04EE: animation "PedEventsMod" loaded
    then
        04EF: release_animation "PedEventsMod"
    end
    0E40: get_current_hour 2@
    0D59: get_current_weather_to 1@
    if or
        2@ < 7
        2@ > 18
        1@ == 8 // not weather rain
        1@ == 16 // not weather rain 2
        not $ACTIVE_INTERIOR == 0
    then
        0A93: terminate_this_custom_script
    end
    if and
        8154:   not actor $PLAYER_ACTOR in_zone 'LDOC'
        8154:   not actor $PLAYER_ACTOR in_zone 'LAIR'
        8154:   not actor $PLAYER_ACTOR in_zone 'SFAIR'
        8154:   not actor $PLAYER_ACTOR in_zone 'EASB'
        8154:   not actor $PLAYER_ACTOR in_zone 'VAIR'
        8154:   not actor $PLAYER_ACTOR in_zone 'KACC'
    then
        0A93: terminate_this_custom_script
    end
end
return
